﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataContexts.Supplier;

public partial class Supplier_Location_AddEdit : System.Web.UI.Page
{
    private SupplierDataContext supplierContext = new SupplierDataContext();

    private int SupplierId
    {
        get { return Helper.QSIntValue("sid"); }
    }
    private int SupplierLocationId
    {
        get { return Helper.QSIntValue("id"); }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        ((BaseMasterPage)this.Master).CurrentPage = PageConfig.Pages.SupplierLocationAddEdit;
        if (!((BaseMasterPage)this.Master).AuthorizeCurrentUser())
            return;

        if (!IsPostBack)
        {
            LoadSuppliers();
            LoadLocations();
            trSupplier.Visible = UserAccess.IsCurrentUser(Enums.UserTypes.Admin);
            if (SupplierLocationId > 0)
            {
                SupplierDataContext context = new SupplierDataContext();
                SupplierLocation supplierlocation = context.SupplierLocations.SingleOrDefault(obj => obj.SupplierLocationId == SupplierLocationId);

                //If user is not admin and try to access other location details the current logged in supplier
                //will be redirected to default listing page of locations
                if (!UserAccess.IsCurrentUser(Enums.UserTypes.Admin) && supplierlocation.SupplierId != UserAccess.CurrentSupplier.SupplierId)
                    Response.Redirect("default.aspx", true);

                //CONTROL ASSIGNMENT
                ddlSupplier.SelectedValue = supplierlocation.SupplierId.ToString();
                ddlLocation.SelectedValue = supplierlocation.LocationId.ToString();
                txtAddress.Text = supplierlocation.Address;
                txtCity.Text = supplierlocation.City;
                txtPostCode.Text = supplierlocation.PostCode;
                txtPhone.Text = supplierlocation.Phone;
                txtMobile.Text = supplierlocation.Mobile;
                txtInstruction.Text = supplierlocation.Instruction;
                chkActive.Checked = supplierlocation.Active.Value;
            }
        }
    }

    private void LoadLocations()
    {
        ddlLocation.DataSource = supplierContext.Locations;
        ddlLocation.DataTextField = "LocationName";
        ddlLocation.DataValueField = "LocationId";
        ddlLocation.DataBind();

        ddlLocation.Items.Insert(0, new ListItem("Select Location", "0"));
    }

    private void LoadSuppliers()
    {
        ddlSupplier.DataSource = supplierContext.Suppliers;
        ddlSupplier.DataTextField = "SupplierName";
        ddlSupplier.DataValueField = "SupplierId";
        ddlSupplier.DataBind();

        ddlSupplier.Items.Insert(0, new ListItem("Select Supplier", "0"));

        if (!trSupplier.Visible)
            ddlSupplier.SelectedValue = UserAccess.CurrentSupplier.SupplierId.ToString();
        else
            ddlSupplier.SelectedValue = SupplierId.ToString();
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        SupplierDataContext context = new SupplierDataContext();
        SupplierLocation supplierlocation = new SupplierLocation();
        if (SupplierLocationId > 0)
            supplierlocation = context.SupplierLocations.SingleOrDefault(obj => obj.SupplierLocationId == SupplierLocationId);

        supplierlocation.LocationId = Convert.ToInt32(ddlLocation.SelectedValue);
        supplierlocation.Address = txtAddress.Text;
        supplierlocation.City = txtCity.Text;
        supplierlocation.PostCode = txtPostCode.Text;
        supplierlocation.Phone = txtPhone.Text;
        supplierlocation.Mobile = txtMobile.Text;
        supplierlocation.Instruction = txtInstruction.Text;
        supplierlocation.Active = chkActive.Checked;

        supplierlocation.SupplierId = Convert.ToInt32(ddlSupplier.SelectedValue);

        if (SupplierLocationId == 0)
            context.SupplierLocations.InsertOnSubmit(supplierlocation);

        context.SubmitChanges();

        Response.Redirect("default.aspx");
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("default.aspx");
    }
}
